package dbx

import (
	"context"
	"github.com/redis/go-redis/v9"
	"log"
	"strconv"
	"time"
)

func InitRedis(config DataBase) (*redis.Client, error) {
	atoi, err := strconv.Atoi(config.DataBase)
	if err != nil {
		return nil, err
	}
	return CreatRedisCon(config.Username, config.Password, config.Host, atoi), nil
}
func CreatRedisCon(username, password, Addr string, db int) *redis.Client {
	redisClient := redis.NewClient(&redis.Options{
		Username:    username,
		Addr:        Addr,
		Password:    password,
		DB:          db,
		PoolSize:    10,               // 最大连接数
		DialTimeout: time.Second * 10, // 超时时间
	})
	str, err := redisClient.Ping(context.Background()).Result()
	if err != nil {
		panic(err)
	}
	log.Println("", str)
	return redisClient
}
